home *** CD-ROM | disk | FTP | other *** search
/ Utilities Professional 1-1500 / Utilities Professional 1-1500 (1994)(WPD)[!].iso / 12511500 / var1453.dms / var1453.adf / AmigaDOS / Example8.c < prev    next >
C/C++ Source or Header  |  1992-05-02  |  4KB  |  127 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM System                  Amiga C Club       */
  7. /* Chapter: AmigaDOS                    Tulevagen 22       */
  8. /* File:    Example8.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-05-02                                       */
  11. /* Version: 1.10                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This program takes a directory/device name as parameter, and    */
  21. /* prints out all the file/directory-names inside it. This example */
  22. /* describes how to use Examine() and ExNext().                    */
  23.  
  24.  
  25. #include <libraries/dos.h>
  26. #include <exec/memory.h>
  27.  
  28.  
  29. main( argc, argv )
  30. int argc;
  31. char *argv[];
  32. {
  33.   struct FileLock *lock;
  34.   struct FileInfoBlock *fib_ptr; /* Declare a FileInfoBlock */
  35.                                  /* pointer called fib_ptr. */
  36.  
  37.  
  38.   if( argc < 2 )
  39.   {
  40.     /* No directory/device specified! */
  41.     printf("Which directory/device do you actually want to examine?\n");
  42.     exit();
  43.   }
  44.  
  45.  
  46.  
  47.   /* Allocate enough memory for a FileInfoBlock structure: */
  48.   fib_ptr = (struct FileInfoBlock *)
  49.             AllocMem( sizeof( struct FileInfoBlock ),
  50.                       MEMF_PUBLIC | MEMF_CLEAR );
  51.  
  52.   /* Check if we have allocated the memory successfully: */
  53.   if( fib_ptr == NULL )
  54.   {
  55.     printf("Not enough memory!\n");
  56.     exit();
  57.   };
  58.  
  59.  
  60.   
  61.   /* Try to lock the file: */
  62.   lock = (struct FileLock *) Lock( argv[ 1 ], SHARED_LOCK );
  63.   
  64.   /* Colud we lock the file? */
  65.   if( lock == NULL )
  66.   {
  67.     printf("Could not lock the file/directory!\n");
  68.  
  69.     /* Deallocate the memory we have allocated: */
  70.     FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );
  71.     
  72.     exit();
  73.   }
  74.  
  75.  
  76.  
  77.   /* Try to examine the directory/device/(file): */
  78.   if( Examine( lock, fib_ptr ) )
  79.   {
  80.     /* Check if it is a directory/device: */
  81.     if( fib_ptr->fib_DirEntryType > 0 )
  82.     {
  83.       /* Print out the directory/device name with underlined characters: */
  84.       /* \033[4m : Underline */
  85.       /* \033[0m : Normal    */
  86.       printf("\033[4m%s\033[0m\n", fib_ptr->fib_FileName );
  87.  
  88.  
  89.  
  90.       /* As long as we can examine files/directories we continue: */
  91.       while( ExNext( lock, fib_ptr ) )
  92.       {
  93.         /* If it is a file we print out the name with white characters. */
  94.         /* However, if it is a (sub)directory we use orange:            */
  95.         if( fib_ptr->fib_DirEntryType < 0 )
  96.           printf("%s\n", fib_ptr->fib_FileName ); /* File */
  97.         else
  98.           printf("\033[33m%s\033[31m\n", fib_ptr->fib_FileName ); /* Dir */
  99.  
  100.         /* \033[33m : Orange (Colour 3) */
  101.         /* \033[31m : White  (Colour 1) */
  102.       }
  103.  
  104.  
  105.  
  106.       /* Check what went wrong. If it was not because there were no more */
  107.       /* files in the directory (ERROR_NO_MORE_ENTRIES), something       */
  108.       /* terrible has happened!                                          */
  109.       if( IoErr() != ERROR_NO_MORE_ENTRIES )
  110.         printf("ERROR WHILE READING!!!\n");
  111.     }
  112.     else
  113.       printf("%s is a file!\n", argv[1] );
  114.   }
  115.   else
  116.     printf("Could not examine %s!\n", argv[ 1 ] );
  117.  
  118.  
  119.  
  120.   /* Unlock the file: */
  121.   UnLock( lock );  
  122.  
  123.  
  124.  
  125.   /* Deallocate the memory we have allocated: */
  126.   FreeMem( fib_ptr, sizeof( struct FileInfoBlock ) );
  127. }